package com.gurumades.jsonform.shared.dispatch;

import java.util.Map;

import com.gurumades.jsonform.shared.dispatch.validators.ValidatorHelper;

@SuppressWarnings("rawtypes")
public class FieldHelper {
	static JsonHelper jshelper  = ValidatorHelper.getJsonHelper();;

	public static void setHelper(JsonHelper helper) {
		FieldHelper.jshelper = helper;
	}

	public static boolean isEmpty(Object item, Object value){
		
		Map mapItem = jshelper.wrapItem(item);
		
		String type = jshelper.getString(mapItem, "type", null);
		if (type == null) {
			return true;
		}
		
		if (type.startsWith("text-")) {
			return isStringFieldEmpty(value);
		}
		
		return false;
		
	}
	
	public static boolean isStringFieldEmpty(Object val){
		
		Map mapVal = jshelper.wrapItem(val);
		if (mapVal == null) {
			return true;
		}
		Map inner = (Map)mapVal.get("value");
		String value = jshelper.getString(inner, "value", null);
		if (value == null) {
			return true;
		}
		return value.trim().isEmpty();
	}
	

}
